<template>
  <div class="container">
    <div class="box" :cols="1" x-gap="20" y-gap="20" v-for="(item, index) in goodsOptList" :key="index">
      <div class="">
        <div class="title">
          <p>规格名称</p>
          <div class="flex-center" @click="additem">
            <a-icon name="BookmarkAdd" size="20px" color="#4270cb"></a-icon>
            <p class="clear flex-center" v-if="goodsOptList.length-1 == index" @click.stop="removeItem(index)" style="margin-left: 10px">
              <a-icon name="RowDelete" size="20px" color="#fb5430"></a-icon>
            </p>
          </div>
        </div>
        <n-input v-model:value="item.title" round type="text" placeholder="请输入规格名称" />
      </div>
      <n-divider />
      <div class="title">
        <p>细分规格</p>
        <p class="flex-center" @click="addopt(index)">
          <a-icon name="AddAlt" size="17px" color="#4270cb"></a-icon>
          添加
        </p>
      </div>
      <div class="options">
        <div class="options_inner" v-for="(opt, i) in item.options" :key="i">
          <div class="item">
            <span>名称:</span>
            <n-input v-model:value="opt.desc" type="text" round placeholder="细分名称"><template #suffix></template></n-input>
          </div>
          <div class="item">
            <span>价格:</span>
            <n-input v-model:value="opt.price" type="text" round placeholder="细分价格"><template #suffix>元</template></n-input>
          </div>
          <div class="clear" v-if="i" @click="removeOpt(index, i)">
            <a-icon name="Delete" size="14px" color="#fb5430"></a-icon>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script lang="tsx" setup>
import { GoodsAPI } from '@/app/api';
import { reactive, ref } from 'vue';
import { useMessage } from 'naive-ui';
let message = useMessage();
let props = defineProps<{ goodsId: number }>();
let goodsOptList = ref([]);
const additem = () => {
  let optitem = {
    title: '',
    goodsName: '',
    options: [
      {
        desc: '',
        price: ''
      }
    ]
  };
  goodsOptList.value.push(optitem);
};

const addopt = index => {
  let opt = {
    desc: '',
    price: ''
  };
  goodsOptList.value[index].options.push(opt);
};
const removeOpt = (index, i) => {
  goodsOptList.value[index].options.splice(i, 1);
};
const removeItem = (index) => {
 goodsOptList.value.splice(index, 1);
};

const queryGoodsOptList = () => {
  GoodsAPI.goodsOptionsFind({ goodsId: props.goodsId }).then(res => {
    if (res.data.length) {
      goodsOptList.value = res.data;
    } else {
      additem();
    }
  });
};
defineExpose({
  submit() {
    let params = {
      options: goodsOptList.value,
      goodsId: props.goodsId
    };
    return GoodsAPI.goodsOptionsAdd(params);
  }
});

queryGoodsOptList();
</script>
<style lang="scss" scoped>
.n-divider:not(.n-divider--vertical) {
  margin-top: 12px;
  margin-bottom: 8px;
}

.container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
}

.box {
  position: relative;
  width: 45%;
  border: 1px solid #cfcfcf;
  background: #fafafa;
  border-radius: 5px;
  margin: 5px;
  padding: 5px;

  p {
    cursor: pointer;
    height: 30px;
    line-height: 30px;
    margin-left: 10px;
  }

  .title {
    height: 38px;
    display: flex;
    justify-content: space-between;
  }

  .options {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;

    .options_inner {
      position: relative;
      width: 30%;
      padding: 3px;
      margin: 3px;
      border-radius: 5px;
      background-color: rgb(66 112 203 / 6%);

      .clear {
        position: absolute;
        top: -7px;
        right: -5px;
      }

      div {
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
        margin: 2px 0;
        font-size: 10px;

        span {
          display: inline-block;
          width: 35%;
        }
      }
    }
  }
}
</style>
